package com.bq.gametrade.ui.dialog;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.bq.gametrade.R;
import com.bq.gametrade.bean.RedEnvelopeListDetails;
import com.bq.gametrade.ui.activity.RedEnvelopeDetailsActivity;
import com.bq.gametrade.utils.Base64Util;
import com.bumptech.glide.Glide;

import java.util.ArrayList;

/**
 * Created by Feel on 2016/12/23.
 */

public class OpenRedEnvelopeDialog extends DialogFragment implements View.OnClickListener{

   private ImageView ivOpenRedEnvelope,ivBack,frieadImage;


    private TextView tvName,tvLeaveMessage;
    private AnimationDrawable animationDrawable;
    private ArrayList<RedEnvelopeListDetails> mList = new ArrayList<>() ;
    private int position;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mList = getArguments().getParcelableArrayList("redEnvelopeList");
        if(mList.size()<0){
            dismiss();
        }
        position = getArguments().getInt("position");

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        View view = inflater.inflate(R.layout.dialog_receiver_redenvelope, container);
        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

        Window window = getDialog().getWindow();

        WindowManager.LayoutParams lp = window.getAttributes();
        window.setGravity(Gravity.CENTER);
        window.setAttributes(lp);
        getDialog().show();
        initView(view);
        return view;

    }

    private void initView(View view) {
        ivOpenRedEnvelope = (ImageView) view.findViewById(R.id.iv_openRedEnvelope);
        ivOpenRedEnvelope.setOnClickListener(this);
        animationDrawable = (AnimationDrawable) ivOpenRedEnvelope.getBackground();
        ivBack = (ImageView) view.findViewById(R.id.iv_back);
        ivBack.setOnClickListener(this);
        tvName = (TextView) view.findViewById(R.id.tv_name);
        tvName.setText(Base64Util.decodeStr(mList.get(position).send_uname));
        tvLeaveMessage = (TextView) view.findViewById(R.id.tv_LeaveMessage);
        tvLeaveMessage.setText(Base64Util.decodeStr(mList.get(position).title));
        frieadImage = (ImageView) view.findViewById(R.id.civ_headImage);
        Glide.with(getActivity())
                .load(mList.get(position).send_img)
                .error( R.mipmap.user_headimg_gray )
                .into(frieadImage);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.iv_openRedEnvelope:
                if(animationDrawable.isRunning()){
                }else {
                    animationDrawable.start();
                    handler.sendEmptyMessageDelayed(1,800);
                }
                break;
            case R.id.iv_back:
                dismiss();
                break;
        }
    }

    Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if(msg.what == 1){
               Intent intent = new Intent(getActivity(), RedEnvelopeDetailsActivity.class);
                intent.putParcelableArrayListExtra("redEnvelopeList",mList);
                intent.putExtra("position",position);
                startActivity(intent);
                dismiss();
            }
        }
    };

    @Override
    public void onDestroy() {
        super.onDestroy();
        if(animationDrawable != null){
            animationDrawable.stop();
        }
    }
}
